package cn.com.demo.microservice.anquan.service.impl;

import cn.com.demo.microservice.anquan.mapper.AqAccountMapper;
import cn.com.demo.microservice.anquan.mapper.AqUserMapper;
import cn.com.demo.microservice.anquan.service.IAccountService;
import cn.com.demo.microservice.entity.AqAccount;
import cn.com.demo.microservice.entity.AqUser;
import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private AqAccountMapper accountMapper;
    @Autowired
    private AqUserMapper userMapper;

    @Override
    @LcnTransaction(propagation = DTXPropagation.SUPPORTS)
    @Transactional
    public int addAccount(AqAccount account) {
        int accId = -1;

        if(account != null){
            // 校验是否有对应的用户
            AqUser aqUser = this.userMapper.selectByPrimaryKey(account.getAccUrId());
            if(aqUser == null){
                throw new RuntimeException("没有对应的用户");
            }
            // 校验账号是否已经存在
            AqAccount dbAccount = this.accountMapper.selectByCode(account.getAccCode());
            if(dbAccount != null){
                throw new RuntimeException(account.getAccCode()+"已存在");
            }
            this.accountMapper.insert(account);
            accId = account.getAccId();
        }

        return accId;
    }
}
